package cn.tanghom.app.oa.service;

import java.util.Date;

import javax.annotation.Resource;

import lombok.val;

import org.duineframework.service.ClassificationService;
import org.flowable.engine.delegate.BpmnError;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import cn.tanghom.app.oa.controller.CandidateController;
import cn.tanghom.app.oa.model.Enterprise;
import cn.tanghom.app.oa.model.Position;
import cn.tanghom.app.oa.model.PositionSurvey;
import cn.tanghom.app.oa.repository.EnterpriseRepository;
import cn.tanghom.app.oa.repository.PositionRepository;
import cn.tanghom.app.oa.repository.PositionSurveyRepository;
import cn.tanghom.app.oa.repository.TalentRepository;
import cn.tanghom.support.utils.StringUtils;

/**
 * 获取简历信息 
 * @author Hunteron-cp
 *
 */
@Service
public class PositionDataDelegate implements JavaDelegate {
	
  @Resource
  private PositionRepository positionRespository;
  
  @Resource 
  private EnterpriseRepository companyRespository;
  
  @Resource
  private PositionSurveyRepository positionSurveyRepository;
  
  
  @Resource
  private CandidateController candidateController;
  
  @Resource
  private ClassificationService clsService;
  
  
  boolean useBean = true;

  @Override
  public void execute(DelegateExecution execution) {
    Date now = new Date();
    Position position = (Position)execution.getVariable("position");    
    Long positionId = (Long) execution.getVariable("positionId");
    if(positionId==null){
    	execution.setTransientVariable("errorMessage", "not existed!");    	
    	throw new BpmnError("BusinessExceptionOccurred");
    	//return ;
    }
    if(position==null){
    	position = positionRespository.findOne(positionId);
	    if(position==null){
	    	execution.setTransientVariable("errorMessage", "not existed!");    	
	    	throw new BpmnError("BusinessExceptionOccurred");
	    	//return ;
	    }
    }
    
    PositionSurvey survey = positionSurveyRepository.findOne(position.getId());
    if(survey==null){
    	survey = new PositionSurvey(positionId);
    }
    Enterprise company = companyRespository.findOne(position.getCompanyId());
    
    execution.setTransientVariable("position", position);
    execution.setTransientVariable("company", company);
    execution.setTransientVariable("survey", survey);
    
    //一级职能   
    execution.setVariable("titleId", position.getDepName());
    execution.setVariable("titleId2", position.getTitleId());
    
   
    
    if(useBean){
    	/**
	    val bean = candidateController.matchPosition(positionId);
	    
	    
	    build(company,bean);
	    build(survey,bean);
	    
	    if(build(position,bean)){
	    	execution.setTransientVariable("positionBean", bean);
	    }
	    */
    }
    
    String titleId = clsService.predictTitle("title", position.getTitle());
    execution.setVariable("titleId", titleId);
   
    System.out.println("Faux call to backend for ["  + execution.getVariable("positionId") + "]"+ position.getTitle());
  }
  
  /**
  public boolean build(Position pos, McPositionBean bean){
	 
	  return true;
  }
  
  
  public boolean build(Enterprise ent, McPositionBean bean){
	  ent.setGroupEnterpriseIdSet(bean.getGroupEnterpriseIdSet());
	  ent.setName(bean.getEnterpriseName());
	  ent.setDisplayName(bean.getEnterpriseDisplayName());
	  ent.setShortName(bean.getEnterpriseShortName());
	  
	  ent.setBlackList(bean.getBlackList());
	  ent.setCompanyBlackList(bean.getCompanyBlackList());
	  ent.setExcludeCompanyList(StringUtils.stringToList(bean.getExcludeCompany()));
	  ent.setPartnerList(StringUtils.stringToList(bean.getEnterpriseOperation()));
	  return true;
  }
  
  public boolean build(PositionSurvey pos, McPositionBean bean){
	  if(bean.getCollegeRequiredMust()!=null){
		  pos.setCollegeRequired(bean.getCollegeRequiredMust().booleanValue());
	  }
	  if(bean.getAgeMust()!=null){
		  pos.setAgeRequired(bean.getAgeMust());
	  }
	  if(bean.getDegreeMust()!=null){
		  pos.setDegreeRequired(bean.getDegreeMust());
	  }
	  if(bean.getGenderMust()!=null){
		  pos.setGenderRequired(bean.getGenderMust());
	  }
	  if(bean.getWorkExpMust()!=null){
		  pos.setWorkExpRequired(bean.getWorkExpMust());
	  }
	  if(bean.getTargetCompanyMust()!=null){
		  pos.setTargetCompanyRequired(bean.getTargetCompanyMust());
	  }
	  
	  if(bean.getWorkStabilityMust()!=null){
		  pos.setWorkStabilityRequired(bean.getWorkStabilityMust());
	  }
	  
	  if(bean.getFieldDirectionBgReqMust()!=null){
		  pos.setFieldDirectionBgRequired(bean.getWorkStabilityMust());
	  }
	  
	  if(bean.getProductTypeReqMust()!=null){
		  pos.setFieldDirectionBgRequired(bean.getProductTypeReqMust());
	  }	  
	  return true;
  }
  */
}
 